//导入模块
import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/store"
Vue.use(VueRouter);//注册路由器
let login='/login';//登录网址路由

//创建路由器实例
const router = new VueRouter({
    //设置路由表
    routes:[
        {
            path:'/main',
            component:()=>import('@/components/main/Main')
        },
        {
            path:'/login',
            component:()=>import('@/components/login')
        },
/*        {
            path:'/cards',
            component:()=>import('../components/Cards'),
			children:[
			  {
				path:'studentAdd',  //二级路由前面不要带 /
				component:StudentAdd
			  }
			]
        },*/
		
		{
		    path:'/test',
		    component:()=>import('@/components/login')
		},
		{path:'/draw',redirect:'/test'} //重定向
    ],
    mode:'history'//修改路由模式为'history'模式
});


// 注册全局前置导航守卫（登录）
router.beforeEach((to, from, next) => {
	if(store.state.testmode){
		next();
		return;
	}
  // to and from are both route objects. must call `next`.
  if (to.path === login) {//若跳转的路径为登陆页面 
		next();
  } else {//请求的是其他路径:判断是否已经登陆过
    if(sessionStorage.getItem("userLogin") == 'true' 
	|| (document.location.host.indexOf("localhost")!=-1
	||document.location.host.indexOf("127.0.0.1")!=-1)) {
		//判断是否登录
		next();
    } else {//用户未登录
      next({//强制跳转到登录页面
        path: login,
        query: {//查询,重定向至login的完整路径
          redirect: to.fullPath
        }
      })
    }
  }
})

export default router;